<extend name="Home@Base/common" />
<block name="body">
  <div class="span9 page_message">
  <section id="contents"> <include file="Home@Addons/_nav" />
    <div class="tab-content"> 
      <!-- 表单 -->
      <php> $post_url || $post_url = U('add?model='.$model['id'], $get_param);</php>
      <form id="form" action="{$post_url}" method="post" class="form-horizontal" style="overflow:hidden">
      	 <div class="fl" style="width:600px;">
         	<!-- 红包；类型设置 -->
         	<div class="form_set">
            	<div class="form_set_title">选择红包类型</div>
                <div class="controls">
                    <label class="item-label">
                           红包类型
                    </label>
                    <div class="check-item">
                        <input type="radio" class="regular-radio toggle-data" value="{$key}" id="randomPrice" name="type" checked="checked">
                        <label for="randomPrice" onClick="$('#fixedSet').hide();$('#randomSet').show();"></label>随机红包
                        &nbsp;&nbsp;&nbsp;&nbsp;
                        <input type="radio" class="regular-radio toggle-data" value="{$key}" id="fixedPrice" name="type">
                        <label for="fixedPrice" onClick="$('#fixedSet').show();$('#randomSet').hide();"></label>固定金额红包  
                    </div>
                </div>
                <div id="randomSet">
                    <div class="controls">
                        <label class="item-label">
                               最小金额
                        </label>
                        <input type="text" name="min_price" class="text input-large" />
                    </div>
                    <div class="controls">
                        <label class="item-label">
                               最大金额
                        </label>
                        <input type="text" name="max_price" class="text input-large" />
                    </div>
                </div>
                <div id="fixedSet" style="display:none">
                    <div class="controls">
                        <label class="item-label">
                               红包金额
                        </label>
                        <input type="text" name="fixed_price" class="text input-large" />
                    </div>
                </div>
            </div>
            <!-- 红包信息 -->
            <div class="form_set">
            	<div class="form_set_title">填写红包信息</div>
                <div class="controls">
                    <label class="item-label">
                           活动名称
                    </label>
                    <input type="text" name="act_name" class="text input-large" />
                </div>
                <div class="controls">
                    <label class="item-label">
                           商户名称
                    </label>
                    <input type="text" name="send_name" class="text input-large" />
                </div>
                <div class="controls">
                    <label class="item-label">
                           备注
                    </label>
                    <input type="text" name="remark" class="text input-large" />
                </div>
                <div class="controls">
                    <label class="item-label">
                           祝福语
                    </label>
                    <textarea name="wishing" class="text" style="width:405px; height:100px;"></textarea>
                </div>
            </div>
            <!-- 红包信息 -->
            <div class="form_set">
            	<div class="form_set_title">配置红包分享信息</div>
                <div class="controls">
                    <label class="item-label">
                           上传用户LOGO
                    </label>
                    <div class="controls uploadrow2" data-max="1" title="点击修改图片" rel="user_logo">
                      <input type="file" id="upload_picture_user_logo">
                      <input type="hidden" name="user_logo" id="cover_id_user_logo"/>
                      <div class="upload-img-box" id="cover_id_user_logo_img" style="display:none">
                             <img src=""  width="100" height="100"/>
                            <em class="edit_img_icon">&nbsp;</em>
                      </div>
                      
                      
                      <script type="text/javascript">
                            //上传图片
                            /* 初始化上传插件 */
                            $("#upload_picture_user_logo").uploadify({
                                "height"          : 100,
                                "swf"             : STATIC+"/uploadify/uploadify.swf",
                                "fileObjName"     : "download",
                                "buttonText"      : "上传图片",
                                "uploader"        : UPLOAD_PICTURE,
                                "width"           : 100,
                                'removeTimeout'	  : 1,
                                'fileTypeExts'	  : '*.jpg; *.png; *.gif;',
                                "onUploadSuccess" : uploadPictureuser_logo
                            });
                            function uploadPictureuser_logo(file, data){
                                console.log(data);
                                var data = $.parseJSON(data);
                                var src = '';
                                if(data.status){										
                                    src = data.url || ROOT + data.path;
                                    $('#cover_id_user_logo_img').show().find('img').attr('src',src);
                                    $('input[name="user_logo"]').val(data.id);
                                    $(window.frames["wxIframe"].document).find(".seller img").attr('src',src).show();;
                                } else {
                                    updateAlert(data.info);
                                    setTimeout(function(){
                                        $('#top-alert').find('button').click();
                                        $(that).removeClass('disabled').prop('disabled',false);
                                    },1500);
                                }
                            }
                        </script> 
                    </div>
                </div>
                <div class="controls">
                    <label class="item-label">
                           分享的文案
                    </label>
                    <input type="text" name="share_title" class="text input-large" />
                </div>
                <div class="controls">
                    <label class="item-label">
                           分享的图片
                    </label>
                    <div class="controls uploadrow2" data-max="1" title="点击修改图片" rel="shop_logo">
                      <input type="file" id="upload_picture_shop_logo">
                      <input type="hidden" name="shop_logo" id="cover_id_shop_logo"/>
                      <div class="upload-img-box" id="cover_id_shop_logo_img" style="display:none">
                             <img src=""  width="100" height="100"/>
                            <em class="edit_img_icon">&nbsp;</em>
                      </div>
                      
                      
                      <script type="text/javascript">
                            //上传图片
                            /* 初始化上传插件 */
                            $("#upload_picture_shop_logo").uploadify({
                                "height"          : 100,
                                "swf"             : STATIC+"/uploadify/uploadify.swf",
                                "fileObjName"     : "download",
                                "buttonText"      : "上传图片",
                                "uploader"        : UPLOAD_PICTURE,
                                "width"           : 100,
                                'removeTimeout'	  : 1,
                                'fileTypeExts'	  : '*.jpg; *.png; *.gif;',
                                "onUploadSuccess" : uploadPictureshop_logo
                            });
                            function uploadPictureshop_logo(file, data){
                                console.log(data);
                                var data = $.parseJSON(data);
                                var src = '';
                                if(data.status){										
                                    src = data.url || ROOT + data.path;
                                    $('#cover_id_shop_logo_img').show().find('img').attr('src',src);
                                    $('input[name="shop_logo"]').val(data.id);
                                    $(window.frames["wxIframe"].document).find(".seller img").attr('src',src).show();;
                                } else {
                                    updateAlert(data.info);
                                    setTimeout(function(){
                                        $('#top-alert').find('button').click();
                                        $(that).removeClass('disabled').prop('disabled',false);
                                    },1500);
                                }
                            }
                        </script> 
                    </div>
                </div>
                <div class="controls">
                    <label class="item-label">
                           分享的链接
                    </label>
                    <textarea name="share_link" class="text" style="width:405px; height:70px;"></textarea>
                </div>
            </div>
          </div> 
            
        	
    
      
          <div class="fl" style="width:400px; margin-top:27px;">
                <div class="wx-header"><span>领取红包</span></div>
                <!-- 手机端页面 -->
                <div id="container" class="wx-container">
                      <iframe name="wxIframe" frameborder="0" scrolling="no" width="320" height="560" src="{:addons_url('RedBag://RedBag/package')}"></iframe>
                	  <div class="frame_layer"></div>
                </div>
                <!-- 手机端上页面结束 -->
          </div>
      
      </div>
      <div class="form-item form_bh" style="text-align:center">
          <button class="btn submit-btn ajax-post" id="submit" type="submit" target-form="form-horizontal">提交</button>
      </div>
    </form>
    
    </div>
  </section>
  </div>
</block>
<block name="script">
	
    <link href="__STATIC__/datetimepicker/css/datetimepicker.css?v={:SITE_VERSION}" rel="stylesheet" type="text/css">
   <php>if(C('COLOR_STYLE')=='blue_color') echo '
   <link href="__STATIC__/datetimepicker/css/datetimepicker_blue.css?v={:SITE_VERSION}" rel="stylesheet" type="text/css">
    ';</php>
  <link href="__STATIC__/datetimepicker/css/dropdown.css?v={:SITE_VERSION}" rel="stylesheet" type="text/css">
  <script type="text/javascript" src="__STATIC__/datetimepicker/js/bootstrap-datetimepicker.js"></script> 
  <script type="text/javascript" src="__STATIC__/datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js?v={:SITE_VERSION}" charset="UTF-8"></script> 
  <script type="text/javascript">
$('#submit').click(function(){
    $('#form').submit();
});

$(function(){
    $('.time').datetimepicker({
        format: 'yyyy-mm-dd hh:ii',
        language:"zh-CN",
        minView:0,
        autoclose:true
    });
    $('.date').datetimepicker({
        format: 'yyyy-mm-dd',
        language:"zh-CN",
        minView:2,
        autoclose:true
    });	
	showTab();
	$('.toggle-data').each(function(){
		var data = $(this).attr('toggle-data');
		if(data=='') return true;		
		
	     if($(this).is(":selected") || $(this).is(":checked")){
			 change_event(this)
		 }
	});
	
	$('select').change(function(){
		$('.toggle-data').each(function(){
			var data = $(this).attr('toggle-data');
			if(data=='') return true;		
			
			 if($(this).is(":selected") || $(this).is(":checked")){
				 change_event(this)
			 }
		});
	});
	//编辑页面预览
	$('input[name="title"]').keyup(function(){
		var val = $(this).val();
		$(window.frames["wxIframe"].document).find("#title").text(val);
	})
	$('input[name="shop_name"]').keyup(function(){
		var val = $(this).val();
		$(window.frames["wxIframe"].document).find(".name").text(val);
	})
	$('input[name="use_start_time"]').on('changeDate', function(ev){
		$(window.frames["wxIframe"].document).find(".use_start_time").text($(this).val());
	});
	$('input[name="over_time"]').on('changeDate', function(ev){
		$(window.frames["wxIframe"].document).find(".over_time").text($(this).val());
	});
});
function buttonColorChange(color){
	$(window.frames["wxIframe"].document).find(".start_btn").css('background-color',color);
}
function bgColorChange(color){
	$(window.frames["wxIframe"].document).find(".head_hd").css('background-color',color);
	$(window.frames["wxIframe"].document).find(".head_pic").hide();	
	$('input[name="background"]').val("");
}
//添加地址
var addnewShopUrl = "{:addons_url('Coupon://Shop/add?model=coupon_shop')}";
function chooseAddress(){
	var $shopHtml = $('<div class="chooseShopDialog"><ul><center><img src="__IMG__/loading.gif"/></center></ul><br/><a href="javascript:;" id="addNewShopBtn" class="border-btn">增加新门店</a><div class="m15"><a href="javascript:;" class="btn" id="confirmBtn">确定</a></div></div>');
	$.Dialog.open("添加适用门店",{width:600,height:500},$shopHtml);
	$('#addNewShopBtn',$shopHtml).click(function(){
		window.open(addnewShopUrl);
		$.Dialog.close();
	})
	var ids = [];
	$('.shop_tr').each(function(index, element) {
        var _id = $(this).find('input').val();
		ids[index] = _id;
    });
	$.ajax({
		url:"{:addons_url('Coupon://Shop/list_data',array('p'=>1))}",
		data:{},
		dataType:'JSON',
		success:function(data){
			var html = "";
			var list_data = data.list_data;
			if(list_data && list_data.length>0){
				for(var i=0;i<list_data.length;i++){
					//console.log(list_data[i].name)
					var id = list_data[i].id;
					var name = list_data[i].name;
					var address = list_data[i].address;
					//console.log(ids)
					//console.log(id)
					if(ids.indexOf(id)!=-1){
						html += '<li><input type="checkbox" checked="true" class="shop_id" value="'+id+'"/><span class="name">'+name+'</span><span class="address">'+address+'</span></li>';
					}else{
						html += '<li><input type="checkbox" class="shop_id" value="'+id+'"/><span class="name">'+name+'</span><span class="address">'+address+'</span></li>';
					}
				}
				if(html!=""){
					$('ul',$shopHtml).html(html);
					$('#shopList').show();
				}
			}else{
				$('ul',$shopHtml).html("<center>你没有添加任何门店</center>");
			}
		}
	})
	$('#confirmBtn',$shopHtml).click(function(){
		var selectHtml = "";
		var count = 0;
		var prevLiHtml = "";
		$('li',$shopHtml).each(function(index, element) {
			if($(this).find('.shop_id').prop("checked")==true){
				count++;
				var id = $(this).find('.shop_id').val();
				var name = $(this).find('.name').text();
				var address = $(this).find('.address').text();
				selectHtml += '<tr class="shop_tr">'+
								'<td><input type="hidden" name="shop_id[]" value="'+id+'"/>'+name+'</td>'+
								'<td>'+address+'</td>'+
								'<td><a href="javascript:;" onclick="removeSingleAddress(this)">删除</a></td>'+
							'</tr>';
				prevLiHtml = '<li class="item single_address">'+
                        '<span class="title">'+name+'</span><br/>'+
                         '<a href="#">'+address+'</a>'+
                        '<a href="#"><em>&nbsp;</em></a>'+
                    '</li>';
			}
        });
		$('#shopList tbody').html(selectHtml);
		$(window.frames["wxIframe"].document).find(".v_nav .item").eq(0).siblings().remove();
		if(count==1){
			$(window.frames["wxIframe"].document).find(".v_nav").append(prevLiHtml);
		}else if(count>1){
			var prevHtml = '<a class="item" href="#">适用门店<em>&nbsp;</em></a>';
			$(window.frames["wxIframe"].document).find(".v_nav").append(prevHtml);
		}
		$.Dialog.close();
	})
	
}
function removeSingleAddress(_this){
	$(_this).parents('tr').remove();
	if($('.shop_tr').size()==0){
		$(window.frames["wxIframe"].document).find(".v_nav .item").eq(0).siblings().remove();
	}
}
</script> 
</block>
